<script lang="ts" context="module">
  import type { Load } from "@sveltejs/kit/types/internal";
  import { sl3 } from "$lib/clients/sl3";

  export const load: Load = async({ fetch, params, stuff }) => {
    const scopeId = parseInt(params.scope.split("-")[0]);

    let interval = parseInterval(params.interval, new Date());
    if (interval == null) {
      // The Reapers are done by then anyway...
      interval = {min: new Date("2000-01-01T00:00:00Z"), max: new Date("2187-01-01T00:00:00Z")}
    }

    const sprints = await sl3(fetch, stuff.idToken).listSprints(scopeId, interval);

    return {
      stuff: { 
        title: "Sprints",
      },
      props: {
        sprints,
        intervalString: params.interval,
      },
    };
	}
</script>

<script lang="ts">
  import { goto } from "$app/navigation";
  import Main from "$lib/components/layout/Main.svelte";
  import DeletionModal from "$lib/modals/DeletionModal.svelte";
  import ItemAcquireModal from "$lib/modals/ItemAcquireModal.svelte";
  import ItemCreateModal from "$lib/modals/ItemCreateModal.svelte";
  import HistorySelector from "$lib/components/common/HistorySelector.svelte";
  import { getScopeContext } from "$lib/components/contexts/ScopeContext.svelte";
  import parseInterval from "$lib/utils/timeinterval";
  import type Sprint from "$lib/models/sprint";
  import SprintListContext from "$lib/components/contexts/SprintListContext.svelte";
  import SprintCreateUpdateModal from "$lib/modals/SprintCreateUpdateModal.svelte";
  import SprintSectionList from "$lib/components/scope/SprintSectionList.svelte";

  export let intervalString: string
  export let sprints: Sprint[]

  const {lastHistoryRange} = getScopeContext();

  function changeInterval(e: Event) {
    const target = e.target as HTMLSelectElement;
    lastHistoryRange.set(target.value);

    goto(`./${target.value}`);
  }
</script>

<SprintListContext sprints={sprints} intervalString={intervalString}>
  <Main big>
    <HistorySelector label="Sprints" value={intervalString} on:change={changeInterval} />
    <SprintSectionList />
  </Main>
  
  <ItemCreateModal />
  <ItemAcquireModal />
  <DeletionModal />
  <SprintCreateUpdateModal />
</SprintListContext>
